package cn.edu.neu.vmmigeval.cloud.util;

import cn.edu.neu.vmmigeval.cloud.entity.*;
import cn.edu.neu.vmmigeval.io.ReadUtil;
import cn.edu.neu.vmmigeval.io.WriteUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by yfwz100 on 16/2/25.
 */
public class DatacenterUtil {


    public static void printHosts(DataCenter datacenter) {


        for (Host host : datacenter.getHosts()) {

            System.out.println("host id:" + host.getId() + " host active:" + host.getActive() + "虚拟机个数：" + host.getVmList().size());

        }
        System.out.println("hosts size is :" + datacenter.getHosts().size());

    }

    public static void printVms(DataCenterBroker broker) {


        for (Vm vm : broker.getVms()) {

            System.out.println("vm id:" + vm.getId() + " host id:" + vm.getHost().getId());

        }
        System.out.println("vm size is :" + broker.getVms().size());
    }

    public static void printCloudlets(DataCenterBroker broker) {


        for (Cloudlet cloudlet : broker.getCloudlets()) {

            System.out.println("cloudlet id:" + cloudlet.getId() + " vm id:" + cloudlet.getVmId());

        }
        System.out.println("cloudlets size is :" + broker.getCloudlets().size());
    }


    public static void printTwoHostsChange(int clock, DataCenter datacenter) {


	/*	Host host10 = datacenter.getHosts().get(10);
        Host host11 = datacenter.getHosts().get(11);
		System.out.println("------------"+clock+"----------------");
		System.out.println("主机id:"+host10.getId());
		for (Vm	vm : host10.getVmList()) {
			System.out.println("\t虚拟机："+vm.getId());
			List<Cloudlet> cls = vm.getCloudlets();
			if(vm!=null&&cls!=null&&cls.size()!=0){
				for (Cloudlet cl : cls) {
					System.out.println("\t\t云任务"+cl.getId());
				}
			}
		}
		System.out.println("主机id:"+host11.getId());
		for (Vm	vm : host11.getVmList()) {
			System.out.println("\t虚拟机："+vm.getId());
			List<Cloudlet> cls = vm.getCloudlets();
			if(vm!=null&&cls!=null&&cls.size()!=0){
				for (Cloudlet cl : cls) {
					System.out.println("\t\t云任务"+cl.getId());
				}
			}
		}
		*/

        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;
        for (Host host : datacenter.getHosts()) {
            int currentCpu = 0;
            int currentRam = 0;
            int currentBw = 0;
            int currentDisk = 0;

            if (host.getVmList() != null && host.getVmList().size() != 0) {
                for (Vm vm : host.getVmList()) {

                    currentCpu += vm.getTotalCpu();
                    currentRam += vm.getTotalRam();
                    currentBw += vm.getTotalBw();
                    currentDisk += vm.getTotalDisk();


                }
            }
            System.out.println("host :" + host.getId() + "cpu:" + ((double) currentCpu / host.getTotalCpu()) + " ram:" + ((double) currentRam / host.getTotalRam())
                    + " bw:" + ((double) currentBw / host.getTotalBw()) + " disk:" + ((double) currentDisk / host.getTotalDisk()));

//
            if (((double) currentCpu / host.getTotalCpu()) >= 0.99) {
                num1++;
            }
            if (((double) currentRam / host.getTotalRam()) >= 0.99) {
                num2++;
            }
            if (((double) currentBw / host.getTotalBw()) >= 0.99) {
                num3++;
            }
            if (((double) currentDisk / host.getTotalDisk()) >= 0.99) {
                num4++;
            }

        }
        System.out.println("cpu num is :" + num1);
        System.out.println("ram num is :" + num2);
        System.out.println("bw num is :" + num3);
        System.out.println("disk num is :" + num4);


	/*	for (Host host : datacenter.getHosts()) {

			double result = (double)host.getCurrentCpu()/host.getTotalCpu();
			double result1 = (double)host.getCurrentRam()/host.getTotalRam();
			double result2 = (double)host.getCurrentBw()/host.getTotalBw();
			double result3 = (double)host.getCurrentDisk()/host.getTotalDisk();
			System.out.println("++host id :"+host.getId()+" cpu利用率："+result+" ram利用率："+result1+" bw利用率："+result2+" disk利用率："+result3);

		}*/

		/*for (Host host : datacenter.getHosts()) {

			System.out.println("主机id:"+host.getId());
			for (Vm	vm : host.getVmList()) {
				System.out.println("\t虚拟机："+vm.getId());
				List<Cloudlet> cls = vm.getCloudlets();
				if(vm!=null&&cls!=null&&cls.size()!=0){
					for (Cloudlet cl : cls) {
						System.out.println("\t\t云任务"+cl.getId());
					}
				}
			}
		}*/


        //System.out.println("这是第"+clock+"个周期");


    }

    public static void printFinishedCloudlets(DataCenterBroker broker) {

        for (Cloudlet cl : broker.getFinishedCloudlets()) {
            System.out.println("cloudlet id:" + cl.getId() + " time:" + (cl.getFinishTime() - cl.getStartTime()));

        }


    }


    public static void printDatacenterHosts(DataCenter datacenter) {


        List<Host> hosts = datacenter.getHosts();

        for (Host host : hosts) {

            int currentAllocationCpu = 0;
            int currentAllocationRam = 0;
            int currentAllocationBw = 0;
            int currentAllocationDisk = 0;

            for (Vm vm : host.getVmList()) {
                currentAllocationCpu += vm.getTotalCpu();
                currentAllocationRam += vm.getTotalRam();
                currentAllocationBw += vm.getTotalBw();
                currentAllocationDisk += vm.getTotalDisk();

            }
            System.out.println("host :" + host.getId() + " totalCpu:" + host.getTotalCpu() + " currentCpu:" + currentAllocationCpu
                    + " \ttotalRam:" + host.getTotalRam() + " currentRam:" + currentAllocationRam);

        }
    }

    public static Integer getHostForVm(Vm vm, Map<Integer, List<Integer>> maps) {

        try {
            for (Map.Entry<Integer, List<Integer>> entry : maps.entrySet()) {
                int id = entry.getKey();
                for (Integer myvm : entry.getValue()) {
                    if (myvm == vm.getId()) {
                        return id;
                    }
                }
                //System.out.println(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Map<Integer, List<Integer>> getTotalMap(String guimo) {
        Map<Integer, List<Integer>> maps = null;
        try {
            switch (guimo) {
                case "big":
                    //C:\Users\206\Desktop\mapping
                    maps = ReadUtil.readFromMappingFile("/mapping/big/mapping.txt");
                    break;
                case "middle":
                    maps = ReadUtil.readFromMappingFile("/mapping/middle/mapping.txt");
                    break;
                default:
                    maps = ReadUtil.readFromMappingFile("/mapping/small/mapping.txt");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return maps;
    }

    public static void writeMappingToFile(DataCenter dc) {


        for (Host host : dc.getHosts()) {
            String str = host.getId() + ":";
            for (Vm vm : host.getVmList()) {
                str += vm.getId() + " ";
            }
            str += "\r\n";
            try {
                WriteUtil.writeMappingToFile("C:\\Users\\206\\Desktop\\mapping\\mapping.txt", str);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
